package com.mdxx.xczd.api;

import android.text.TextUtils;

import com.mdxx.xczd.app.App;
import com.mdxx.xczd.app.UrlValue;
import com.mdxx.xczd.utils.FastJsonConverterFactory;
import com.mdxx.xczd.utils.Net;
import com.mdxx.xczd.utils.SPUtil;
import com.orhanobut.logger.Logger;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by MD-02 on 2017/1/13.
 */

public class ApiManage {

    static ApiManage apiManage;
    ApiService apiService;
    private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            if (Net.isNetWorkAvailable(App.getAppContext())) {
                int maxAge = 60; // 在线缓存在1分钟内可读取
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, max-age=" + maxAge)
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 28; // 离线时缓存保存4周
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
            }
        }
    };

    /*private static File httpCacheDirectory = new File(App.getAppContext().getCacheDir(), "zhihuCache");
    private static int cacheSize = 10 * 1024 * 1024; // 10 MiB
    private static Cache cache = new Cache(httpCacheDirectory, cacheSize);*/

    private static OkHttpClient client = new OkHttpClient.Builder()
            .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
           /* .cache(cache)*/
            .build();


    public static ApiManage getInstence() {
        if (apiManage == null) {
            synchronized (ApiManage.class) {
                if (apiManage == null) {
                    apiManage = new ApiManage();
                }
            }
        }
        return apiManage;
    }

    public void initApiService(){
        apiService = null;
    }

    public ApiService getApiService() {
        Logger.d("baseUrl=="+UrlValue.baseUrl);
        if (apiService == null) {
            synchronized (ApiManage.class) {
                if (apiService == null) {
                    apiService = new Retrofit.Builder()
                            .baseUrl(UrlValue.baseUrl)
                            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                            .client(client)
                            /*.addConverterFactory(GsonConverterFactory.create())*/
                            .addConverterFactory(FastJsonConverterFactory.create())
                            .build().create(ApiService.class);
                }
            }
        }
        return apiService;
    }

    public ApiService getApiService1() {
        if(TextUtils.equals(App.firstUrl, "")){
            App.firstUrl = SPUtil.getSharedStringData("first_url");
        }
        Logger.d(" App.firstUrl=="+ App.firstUrl);
        ApiService  apiService1 = new Retrofit.Builder()
                            .baseUrl(App.firstUrl)
                            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                            .client(client)
                            .addConverterFactory(GsonConverterFactory.create())
                            .build().create(ApiService.class);

        return apiService1;
    }
}
